<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Chapter 16: Templates And Generic Programming | A君的秘密基地</title><meta name="keywords" content="C++ Primer"><meta name="author" content="astralcon"><meta name="copyright" content="astralcon"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="# Exercises Section 16.1.1 Ex16.1编译器用推断出的模板参数生成一个特定版本的函数，这个过程就叫做实例化。 Ex16.212345678910111213141516171819202122#include&lt;iostream&gt;#include&lt;functional&gt;using namespace std;template &lt;typena">
<meta property="og:type" content="article">
<meta property="og:title" content="Chapter 16: Templates And Generic Programming">
<meta property="og:url" content="https://astralcon.gitee.io/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/index.html">
<meta property="og:site_name" content="A君的秘密基地">
<meta property="og:description" content="# Exercises Section 16.1.1 Ex16.1编译器用推断出的模板参数生成一个特定版本的函数，这个过程就叫做实例化。 Ex16.212345678910111213141516171819202122#include&lt;iostream&gt;#include&lt;functional&gt;using namespace std;template &lt;typena">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-05-14T06:29:10.000Z">
<meta property="article:modified_time" content="2022-05-14T07:00:12.370Z">
<meta property="article:author" content="astralcon">
<meta property="article:tag" content="C++ Primer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/blog/img/avatar.jpg"><link rel="canonical" href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: {"appId":"YUB0ZFWBBL","apiKey":"78d0f1fae31a1df3e0cb573725c55558","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Chapter 16: Templates And Generic Programming',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-14 15:00:12'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">A君的秘密基地</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Chapter 16: Templates And Generic Programming</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-14T06:29:10.000Z" title="发表于 2022-05-14 14:29:10">2022-05-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-14T07:00:12.370Z" title="更新于 2022-05-14 15:00:12">2022-05-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/C/">C++</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>﻿# Exercises Section 16.1.1</p>
<h2 id="Ex16-1"><a href="#Ex16-1" class="headerlink" title="Ex16.1"></a>Ex16.1</h2><p>编译器用推断出的模板参数生成一个特定版本的函数，这个过程就叫做实例化。</p>
<h2 id="Ex16-2"><a href="#Ex16-2" class="headerlink" title="Ex16.2"></a>Ex16.2</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;functional&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function"><span class="type">int</span> <span class="title">compare</span><span class="params">(<span class="type">const</span> T &amp;v1, <span class="type">const</span> T &amp;v2)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">less</span>&lt;T&gt;() (v1, v2))</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">less</span>&lt;T&gt;() (v2, v1))</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> a, b;</span><br><span class="line">    cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">    cout &lt;&lt; <span class="built_in">compare</span>(a, b) &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-3"><a href="#Ex16-3" class="headerlink" title="Ex16.3"></a>Ex16.3</h2><p>如果你的 compare 函数使用的 &lt; 操作符，那么实例化 Sales_data 会发生错误，因为 Sales_data 未定义 &lt; 操作符。</p>
<h2 id="Ex16-4"><a href="#Ex16-4" class="headerlink" title="Ex16.4"></a>Ex16.4</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;list&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T, <span class="keyword">typename</span> U&gt;</span><br><span class="line"><span class="function">T <span class="title">find</span><span class="params">(T beg, T end, U val)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">auto</span> iter = beg;</span><br><span class="line">    <span class="keyword">while</span> (iter != end)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (*iter == val)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        ++iter;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> iter;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;;</span><br><span class="line">    list&lt;string&gt; ls = &#123;<span class="string">&quot;hello&quot;</span>, <span class="string">&quot;world&quot;</span>, <span class="string">&quot;hi&quot;</span>&#125;;</span><br><span class="line">    <span class="type">int</span> value;</span><br><span class="line">    string word;</span><br><span class="line">    cin &gt;&gt; value &gt;&gt; word;</span><br><span class="line">    <span class="keyword">auto</span> p1 = <span class="built_in">find</span>(vec.<span class="built_in">begin</span>(), vec.<span class="built_in">end</span>(), value);</span><br><span class="line">    <span class="keyword">if</span> (p1 != vec.<span class="built_in">end</span>())</span><br><span class="line">        cout &lt;&lt; *p1 &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">auto</span> p2 = <span class="built_in">find</span>(ls.<span class="built_in">begin</span>(), ls.<span class="built_in">end</span>(), word);</span><br><span class="line">    <span class="keyword">if</span> (p2 != ls.<span class="built_in">end</span>())</span><br><span class="line">        cout &lt;&lt; *p2 &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-5"><a href="#Ex16-5" class="headerlink" title="Ex16.5"></a>Ex16.5</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T, <span class="type">unsigned</span> N&gt; <span class="type">void</span> <span class="title">print</span><span class="params">(<span class="type">const</span> T (&amp;arr)[N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">auto</span> v : arr)</span><br><span class="line">        cout &lt;&lt; v &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> a[] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    string b[] = &#123;<span class="string">&quot;hello&quot;</span>, <span class="string">&quot;world&quot;</span>&#125;;</span><br><span class="line">    <span class="built_in">print</span>(a);</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">print</span>(b);</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-6"><a href="#Ex16-6" class="headerlink" title="Ex16.6"></a>Ex16.6</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T, <span class="type">unsigned</span> N&gt; T* <span class="title">begin</span><span class="params">(<span class="type">const</span> T (&amp;arr)[N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &amp;arr[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T, <span class="type">unsigned</span> N&gt; T* <span class="title">end</span><span class="params">(<span class="type">const</span> T (&amp;arr)[N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &amp;arr[N - <span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> a[] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    string b[] = &#123;<span class="string">&quot;hello&quot;</span>, <span class="string">&quot;world&quot;</span>&#125;;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">auto</span> p = <span class="built_in">begin</span>(a); p != <span class="built_in">end</span>(a); ++p)</span><br><span class="line">        cout &lt;&lt; *p &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">auto</span> p = <span class="built_in">begin</span>(b); p != <span class="built_in">end</span>(b); ++p)</span><br><span class="line">        cout &lt;&lt; *p &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-7"><a href="#Ex16-7" class="headerlink" title="Ex16.7"></a>Ex16.7</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T, <span class="type">unsigned</span> N&gt; <span class="keyword">constexpr</span> <span class="type">unsigned</span> <span class="title">arr_size</span><span class="params">(<span class="type">const</span> T (&amp;arr)[N])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> N;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> a[] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    string b[] = &#123;<span class="string">&quot;hello&quot;</span>, <span class="string">&quot;world&quot;</span>&#125;;</span><br><span class="line">    cout &lt;&lt; <span class="built_in">arr_size</span>(a) &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="built_in">arr_size</span>(b) &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-8"><a href="#Ex16-8" class="headerlink" title="Ex16.8"></a>Ex16.8</h2><p>支持 !&#x3D; 操作的类比支持 &lt; 操作的类更多，简而言之，就是 !&#x3D; 操作更通用。</p>
<h1 id="Exercises-Section-16-1-2"><a href="#Exercises-Section-16-1-2" class="headerlink" title="Exercises Section 16.1.2"></a>Exercises Section 16.1.2</h1><h2 id="Ex16-9"><a href="#Ex16-9" class="headerlink" title="Ex16.9"></a>Ex16.9</h2><p>函数模板：编译器能够根据推断出的模板参数类型实例化一个特定类型的函数。<br>类模板：编译器不能推断出模板参数类型，而是需要在&lt;&gt;内的模板名字后面补充信息，然后实例化一个特定类型的类。</p>
<h2 id="Ex16-10"><a href="#Ex16-10" class="headerlink" title="Ex16.10"></a>Ex16.10</h2><p>当实例化一个类模板时，编译器重写该类模板，通过给定的类型参数取代所有出现模板参数的地方。</p>
<h2 id="Ex16-11"><a href="#Ex16-11" class="headerlink" title="Ex16.11"></a>Ex16.11</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> elemType&gt; <span class="keyword">class</span> <span class="title class_">ListItem</span>;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> elemType&gt; <span class="keyword">class</span> <span class="title class_">List</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">List</span>&lt;elemType&gt;();</span><br><span class="line">    <span class="built_in">List</span>&lt;elemType&gt;(<span class="type">const</span> List&lt;elemType&gt; &amp;);</span><br><span class="line">    List&lt;elemType&gt;&amp; <span class="keyword">operator</span>=(<span class="type">const</span> List&lt;elemType&gt; &amp;);</span><br><span class="line">    ~<span class="built_in">List</span>();</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">insert</span><span class="params">(ListItem *ptr, elemType value)</span></span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    ListItem *front, *end;      <span class="comment">// 类模板的名字不是类型，只有实例化后才是类型</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>代码修改如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> elemType&gt; <span class="keyword">class</span> <span class="title class_">ListItem</span>;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> elemType&gt; <span class="keyword">class</span> <span class="title class_">List</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">List</span>&lt;elemType&gt;();</span><br><span class="line">    <span class="built_in">List</span>&lt;elemType&gt;(<span class="type">const</span> List&lt;elemType&gt; &amp;);</span><br><span class="line">    List&lt;elemType&gt;&amp; <span class="keyword">operator</span>=(<span class="type">const</span> List&lt;elemType&gt; &amp;);</span><br><span class="line">    ~<span class="built_in">List</span>();</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">insert</span><span class="params">(ListItem&lt;elemType&gt; *ptr, elemType value)</span></span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    ListItem&lt;elemType&gt; *front, *end;      </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-12"><a href="#Ex16-12" class="headerlink" title="Ex16.12"></a>Ex16.12</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span>&gt; <span class="keyword">class</span> <span class="title class_">BlobPtr</span>;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> Blob&lt;T&gt; &amp;, <span class="type">const</span> Blob&lt;T&gt; &amp;);</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> Blob&lt;T&gt; &amp;, <span class="type">const</span> Blob&lt;T&gt; &amp;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="keyword">class</span> <span class="title class_">Blob</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="title class_">BlobPtr</span>&lt;T&gt;;</span><br><span class="line">    <span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>==&lt;T&gt;(<span class="type">const</span> Blob&lt;T&gt; &amp;, <span class="type">const</span> Blob&lt;T&gt; &amp;);</span><br><span class="line">    <span class="keyword">friend</span> <span class="type">bool</span> <span class="keyword">operator</span>!=&lt;T&gt;(<span class="type">const</span> Blob&lt;T&gt; &amp;, <span class="type">const</span> Blob&lt;T&gt; &amp;);</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">typedef</span> T value_type;</span><br><span class="line">    <span class="keyword">typedef</span> <span class="keyword">typename</span> std::vector&lt;T&gt;::size_type size_type;</span><br><span class="line">    <span class="built_in">Blob</span>();</span><br><span class="line">    <span class="built_in">Blob</span>(std::initializer_list&lt;T&gt; il);</span><br><span class="line">    <span class="function">size_type <span class="title">size</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> data-&gt;<span class="built_in">size</span>(); &#125;</span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">empty</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> data-&gt;<span class="built_in">empty</span>(); &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">push_back</span><span class="params">(<span class="type">const</span> T &amp;t)</span> </span>&#123; data-&gt;<span class="built_in">push_back</span>(t); &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">push_back</span><span class="params">(T &amp;&amp;t)</span> </span>&#123; data-&gt;<span class="built_in">push_back</span>(std::<span class="built_in">move</span>(t)); &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">pop_back</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function">T&amp; <span class="title">back</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function"><span class="type">const</span> T&amp; <span class="title">back</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">    T&amp; <span class="keyword">operator</span>[](size_type i);</span><br><span class="line">    <span class="type">const</span> T&amp; <span class="keyword">operator</span>[](size_type i) <span class="type">const</span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::shared_ptr&lt;std::vector&lt;T&gt;&gt; data;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">check</span><span class="params">(size_type i, <span class="type">const</span> std::string &amp;msg)</span> <span class="type">const</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">void</span> Blob&lt;T&gt;::<span class="built_in">check</span>(size_type i, <span class="type">const</span> std::string &amp;msg) <span class="type">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (i &gt;= data-&gt;<span class="built_in">size</span>())</span><br><span class="line">        <span class="keyword">throw</span> std::<span class="built_in">out_of_range</span>(msg);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; T&amp; Blob&lt;T&gt;::<span class="built_in">back</span>()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">check</span>(<span class="number">0</span>, <span class="string">&quot;back on empty Blob&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> data-&gt;<span class="built_in">back</span>();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">const</span> T&amp; Blob&lt;T&gt;::<span class="built_in">back</span>() <span class="type">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">check</span>(<span class="number">0</span>, <span class="string">&quot;back on empty Blob&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> data-&gt;<span class="built_in">back</span>();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; T&amp; Blob&lt;T&gt;::<span class="keyword">operator</span>[](size_type i)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">check</span>(i, <span class="string">&quot;subscript out of range&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> (*data)[i];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; T&amp; Blob&lt;T&gt;::<span class="keyword">operator</span>[](<span class="type">size_t</span> i) <span class="type">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">check</span>(i, <span class="string">&quot;subscript out of range&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> (*data)[i];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">void</span> Blob&lt;T&gt;::<span class="built_in">pop_back</span>()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">check</span>(<span class="number">0</span>, <span class="string">&quot;pop_back on empty Blob&quot;</span>);</span><br><span class="line">    data-&gt;<span class="built_in">pop_back</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Blob&lt;T&gt;::<span class="built_in">Blob</span>(): <span class="built_in">data</span>(std::make_shared&lt;std::vector&lt;T&gt;&gt;()) &#123; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Blob&lt;T&gt;::<span class="built_in">Blob</span>(std::initializer_list&lt;T&gt; il): <span class="built_in">data</span>(std::make_shared&lt;std::vector&lt;T&gt;&gt; il) &#123; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> Blob&lt;T&gt; &amp;lhs, <span class="type">const</span> Blob&lt;T&gt; &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (lhs.<span class="built_in">size</span>() != rhs.<span class="built_in">size</span>())</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">size_t</span> i = <span class="number">0</span>; i != lhs.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (lhs[i] != rhs[i])</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> Blob&lt;T&gt; &amp;lhs, <span class="type">const</span> Blob&lt;T&gt; &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> !(lhs == rhs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> BlobPtr&lt;T&gt; &amp;, <span class="type">const</span> BlobPtr&lt;T&gt; &amp;);</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> BlobPtr&lt;T&gt; &amp;, <span class="type">const</span> BlobPtr&lt;T&gt; &amp;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="keyword">class</span> <span class="title class_">BlobPtr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">BlobPtr</span>(): <span class="built_in">curr</span>(<span class="number">0</span>) &#123; &#125;</span><br><span class="line">    <span class="built_in">BlobPtr</span>(Blob&lt;T&gt; &amp;a, <span class="type">size_t</span> sz = <span class="number">0</span>): <span class="built_in">wptr</span>(a.data), <span class="built_in">curr</span>(sz) &#123; &#125;</span><br><span class="line">    T&amp; <span class="keyword">operator</span>*() <span class="type">const</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">auto</span> p = <span class="built_in">check</span>(curr, <span class="string">&quot;dereference past end&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> (*p)[curr];</span><br><span class="line">    &#125;</span><br><span class="line">    BlobPtr &amp; <span class="keyword">operator</span>++();</span><br><span class="line">    BlobPtr &amp; <span class="keyword">operator</span>--();</span><br><span class="line">    BlobPtr <span class="keyword">operator</span>++(<span class="type">int</span>);</span><br><span class="line">    BlobPtr <span class="keyword">operator</span>--(<span class="type">int</span>);</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::shared_ptr&lt;std::vector&lt;T&gt;&gt; <span class="built_in">check</span>(std::<span class="type">size_t</span>, <span class="type">const</span> std::string &amp;) <span class="type">const</span>;</span><br><span class="line">    std::weak_ptr&lt;std::vector&lt;T&gt;&gt; wptr;</span><br><span class="line">    std::<span class="type">size_t</span> curr;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; BlobPtr&lt;T&gt; BlobPtr&lt;T&gt;::<span class="keyword">operator</span>++(<span class="type">int</span>)</span><br><span class="line">&#123;</span><br><span class="line">    BlobPtr ret = *<span class="keyword">this</span>;</span><br><span class="line">    ++*<span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; BlobPtr&lt;T&gt; BlobPtr&lt;T&gt;::<span class="keyword">operator</span>--(<span class="type">int</span>)</span><br><span class="line">&#123;</span><br><span class="line">    BlobPtr ret = *<span class="keyword">this</span>;</span><br><span class="line">    --*<span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>==(<span class="type">const</span> BlobPtr&lt;T&gt; &amp;lhs, <span class="type">const</span> BlobPtr&lt;T&gt; &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> lhs.wptr.<span class="built_in">lock</span>().<span class="built_in">get</span>() == rhs.wptr.<span class="built_in">lock</span>().<span class="built_in">get</span>() &amp;&amp; lhs.curr == rhs.curr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">bool</span> <span class="keyword">operator</span>!=(<span class="type">const</span> BlobPtr&lt;T&gt; &amp;lhs, <span class="type">const</span> BlobPtr&lt;T&gt; &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> !(lhs == rhs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-13"><a href="#Ex16-13" class="headerlink" title="Ex16.13"></a>Ex16.13</h2><p>由于函数模板的实例化只处理特定类型，因此对于相等和关系运算符，对每个 BlobPtr 实例和用相同类型实例化的运算符建议一对一的友好关系即可。</p>
<h2 id="Ex16-14"><a href="#Ex16-14" class="headerlink" title="Ex16.14"></a>Ex16.14</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; <span class="keyword">class</span> <span class="title class_">Screen</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Screen</span>(): <span class="built_in">contents</span>(H * W, <span class="string">&#x27; &#x27;</span>) &#123; &#125;</span><br><span class="line">    <span class="built_in">Screen</span>(<span class="type">char</span> c): <span class="built_in">contents</span>(H * W, c) &#123; &#125;</span><br><span class="line">    <span class="function"><span class="type">char</span> <span class="title">get</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> contents[cursor]; &#125;</span><br><span class="line">    <span class="function"><span class="keyword">inline</span> <span class="type">char</span> <span class="title">get</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span> <span class="type">const</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">move</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">set</span><span class="params">(<span class="type">char</span>)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">display</span><span class="params">(ostream &amp;os)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">do_display</span>(os);</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">const</span> Screen &amp;<span class="title">display</span><span class="params">(ostream &amp;os)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">do_display</span>(os);</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> cursor = <span class="number">0</span>;</span><br><span class="line">    string contents;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">do_display</span><span class="params">(ostream &amp;os)</span> <span class="type">const</span> </span>&#123; os &lt;&lt; contents; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; <span class="keyword">inline</span> <span class="type">char</span> Screen&lt;H, W&gt;::<span class="built_in">get</span>(<span class="type">int</span> r, <span class="type">int</span> c) <span class="type">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> row = r * W;</span><br><span class="line">    <span class="keyword">return</span> contents[row + c];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; <span class="keyword">inline</span> Screen&lt;H, W&gt; &amp;Screen&lt;H, W&gt;::<span class="built_in">set</span>(<span class="type">char</span> c)</span><br><span class="line">&#123;</span><br><span class="line">    contents[cursor] = c;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; <span class="keyword">inline</span> Screen&lt;H, W&gt; &amp;Screen&lt;H, W&gt;::<span class="built_in">move</span>(<span class="type">int</span> r, <span class="type">int</span> c)</span><br><span class="line">&#123;</span><br><span class="line">    cursor = r * W + c;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-15"><a href="#Ex16-15" class="headerlink" title="Ex16.15"></a>Ex16.15</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; istream &amp; <span class="keyword">operator</span>&gt;&gt;(istream &amp;, Screen&lt;H, W&gt; &amp;);</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; ostream &amp; <span class="keyword">operator</span>&lt;&lt;(ostream &amp;, Screen&lt;H, W&gt; &amp;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; <span class="keyword">class</span> <span class="title class_">Screen</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">friend</span> istream &amp; <span class="keyword">operator</span>&gt;&gt;&lt;H, W&gt;(istream &amp;, Screen&lt;H, W&gt; &amp;);</span><br><span class="line">    <span class="keyword">friend</span> ostream &amp; <span class="keyword">operator</span>&lt;&lt;&lt;H, W&gt;(ostream &amp;, Screen&lt;H, W&gt; &amp;);</span><br><span class="line">    <span class="comment">// 其他成员不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; istream &amp; <span class="keyword">operator</span>&gt;&gt;(istream &amp;is, Screen&lt;H, W&gt; &amp;sc)</span><br><span class="line">&#123;</span><br><span class="line">    string s;</span><br><span class="line">    is &gt;&gt; s;</span><br><span class="line">    sc.contents = s.<span class="built_in">substr</span>(<span class="number">0</span>, H * W);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="type">int</span> H, <span class="type">int</span> W&gt; ostream &amp; <span class="keyword">operator</span>&lt;&lt;(ostream &amp;os, Screen&lt;H, W&gt; &amp;sc)</span><br><span class="line">&#123;</span><br><span class="line">    os &lt;&lt; sc.contents;</span><br><span class="line">    <span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-16"><a href="#Ex16-16" class="headerlink" title="Ex16.16"></a>Ex16.16</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="keyword">class</span> <span class="title class_">Vec</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Vec</span>(): <span class="built_in">elements</span>(<span class="literal">nullptr</span>), <span class="built_in">first_free</span>(<span class="literal">nullptr</span>), <span class="built_in">cap</span>(<span class="literal">nullptr</span>) &#123; &#125;</span><br><span class="line">	<span class="built_in">Vec</span>(<span class="type">const</span> Vec&amp;);</span><br><span class="line">	<span class="built_in">Vec</span>(<span class="type">const</span> T*, <span class="type">const</span> T*);</span><br><span class="line">	<span class="built_in">Vec</span>(initializer_list&lt;T&gt; lst);</span><br><span class="line">	Vec&amp; <span class="keyword">operator</span>=(<span class="type">const</span> Vec&amp;);</span><br><span class="line">	~<span class="built_in">Vec</span>();</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">push_back</span><span class="params">(<span class="type">const</span> T&amp;)</span></span>;</span><br><span class="line">	<span class="function"><span class="type">size_t</span> <span class="title">size</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> first_free - elements; &#125;</span><br><span class="line">	<span class="function"><span class="type">size_t</span> <span class="title">capacity</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> cap - elements; &#125;</span><br><span class="line">	<span class="function">T *<span class="title">begin</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> elements; &#125;</span><br><span class="line">	<span class="function">T *<span class="title">end</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> first_free;&#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">reserve</span><span class="params">(<span class="type">size_t</span> n)</span> </span>&#123; <span class="keyword">if</span> (n &gt; <span class="built_in">capacity</span>()) <span class="built_in">resize</span>(n); &#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">resize</span><span class="params">(<span class="type">size_t</span>)</span></span>;</span><br><span class="line">	T&amp; <span class="keyword">operator</span>[](<span class="type">size_t</span> n) &#123; <span class="keyword">return</span> elements[n]; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">static</span> allocator&lt;T&gt; alloc;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">chk_n_alloc</span><span class="params">()</span> </span>&#123; <span class="keyword">if</span> (<span class="built_in">size</span>() == <span class="built_in">capacity</span>() ) <span class="built_in">reallocate</span>(); &#125; </span><br><span class="line">	<span class="function">pair&lt;T*, T*&gt; <span class="title">alloc_n_copy</span><span class="params">(<span class="type">const</span> T*, <span class="type">const</span> T*)</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">free</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">reallocate</span><span class="params">()</span></span>;</span><br><span class="line">	T *elements;</span><br><span class="line">	T *first_free;</span><br><span class="line">	T *cap;	</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; std::allocator&lt;T&gt; Vec&lt;T&gt;::alloc;  </span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">void</span> Vec&lt;T&gt;::<span class="built_in">push_back</span>(<span class="type">const</span> T &amp;s)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">chk_n_alloc</span>();</span><br><span class="line">	alloc.<span class="built_in">construct</span>(first_free++, s);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; pair&lt;T*, T*&gt; Vec&lt;T&gt;::<span class="built_in">alloc_n_copy</span>(<span class="type">const</span> T *b, <span class="type">const</span> T *e)</span><br><span class="line">&#123;</span><br><span class="line">	T *data = alloc.<span class="built_in">allocate</span>(e - b);</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">make_pair</span>(data, <span class="built_in">uninitialized_copy</span>(b, e, data));</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">void</span> Vec&lt;T&gt;::<span class="built_in">free</span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (elements) &#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">auto</span> p = first_free; p != elements; )</span><br><span class="line">			alloc.<span class="built_in">destroy</span>(--p);</span><br><span class="line">		alloc.<span class="built_in">deallocate</span>(elements, cap - elements);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Vec&lt;T&gt;::<span class="built_in">Vec</span>(<span class="type">const</span> Vec&lt;T&gt; &amp;s)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> newdata = <span class="built_in">alloc_n_copy</span>(s.<span class="built_in">begin</span>(), s.<span class="built_in">end</span>());</span><br><span class="line">	elements = newdata.first;</span><br><span class="line">	first_free = newdata.second;</span><br><span class="line">	cap = newdata.second;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Vec&lt;T&gt;::<span class="built_in">Vec</span>(<span class="type">const</span> T *b, <span class="type">const</span> T *e)</span><br><span class="line">&#123;</span><br><span class="line">	pair&lt;T*, T*&gt; newdata = <span class="built_in">alloc_n_copy</span>(b, e);</span><br><span class="line">	elements = newdata.first;</span><br><span class="line">	first_free = cap = newdata.second;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Vec&lt;T&gt;::<span class="built_in">Vec</span>(initializer_list&lt;T&gt; lst)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> newdata = <span class="built_in">alloc_n_copy</span>(lst.<span class="built_in">begin</span>(), lst.<span class="built_in">end</span>());</span><br><span class="line">	elements = newdata.first;</span><br><span class="line">	first_free = cap = newdata.second;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Vec&lt;T&gt;::~<span class="built_in">Vec</span>() &#123; <span class="built_in">free</span>(); &#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; Vec&lt;T&gt; &amp;Vec&lt;T&gt;::<span class="keyword">operator</span>=(<span class="type">const</span> Vec &amp;rhs)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> newdata = <span class="built_in">alloc_n_copy</span>(rhs.<span class="built_in">begin</span>(), rhs.<span class="built_in">end</span>());</span><br><span class="line">	<span class="built_in">free</span>();</span><br><span class="line">	elements = newdata.first;</span><br><span class="line">	first_free = newdata.second;</span><br><span class="line">	cap = newdata.second;</span><br><span class="line">	<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="type">void</span> Vec&lt;T&gt;::<span class="built_in">reallocate</span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">auto</span> newcapacity = <span class="built_in">size</span>() ? <span class="built_in">size</span>() * <span class="number">2</span> : <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">auto</span> newdata = alloc.<span class="built_in">allocate</span>(newcapacity);</span><br><span class="line">	<span class="keyword">auto</span> dest = newdata;</span><br><span class="line">	<span class="keyword">auto</span> elem = elements;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">size_t</span> i = <span class="number">0</span>; i != <span class="built_in">size</span>(); ++i)</span><br><span class="line">		alloc.<span class="built_in">construct</span>(dest++, std::<span class="built_in">move</span>(*elem++));</span><br><span class="line">	<span class="built_in">free</span>();</span><br><span class="line">	elements = newdata;</span><br><span class="line">	first_free = dest;</span><br><span class="line">	cap = elements + newcapacity;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="type">void</span> Vec&lt;T&gt;::<span class="built_in">resize</span>(<span class="type">size_t</span> n)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (n &gt; <span class="built_in">size</span>()) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (n &gt; <span class="built_in">size</span>())</span><br><span class="line">			<span class="built_in">push_back</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (n &lt; <span class="built_in">size</span>())</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (<span class="built_in">size</span>() &gt; n)</span><br><span class="line">			alloc.<span class="built_in">destroy</span>(--first_free);</span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-16-1-3"><a href="#Exercises-Section-16-1-3" class="headerlink" title="Exercises Section 16.1.3"></a>Exercises Section 16.1.3</h1><h2 id="Ex16-17"><a href="#Ex16-17" class="headerlink" title="Ex16.17"></a>Ex16.17</h2><p>在模板参数列表中，typename 和 class 含义相同，typename 比 class 更直观，因为内置类型也可以作为模板类型参数。当使用作用域运算符<code>::</code> 访问一个模板类型参数的成员时，该成员可能为类型或者静态成员，为了区分这点，规定使用 typename 指明其是一个类型。</p>
<h2 id="Ex16-18"><a href="#Ex16-18" class="headerlink" title="Ex16.18"></a>Ex16.18</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">a) <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T, U, <span class="keyword">typename</span> V&gt; <span class="function"><span class="type">void</span> <span class="title">f1</span><span class="params">(T, U, V)</span></span>;   <span class="comment">// 非法，U是类型参数，必须有关键字 typename</span></span><br><span class="line"><span class="comment">// 修改如下:</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T, <span class="keyword">typename</span> U, <span class="keyword">typename</span> V&gt; <span class="function"><span class="type">void</span> <span class="title">f1</span><span class="params">(T, U, V)</span></span>;</span><br><span class="line"></span><br><span class="line">b) <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function">T <span class="title">f2</span><span class="params">(<span class="type">int</span> &amp;T)</span></span>;      <span class="comment">// 非法，T不能作为参数名</span></span><br><span class="line"><span class="comment">// 修改如下：</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function">T <span class="title">f2</span><span class="params">(T &amp;)</span></span>;</span><br><span class="line"></span><br><span class="line">c) <span class="keyword">inline</span> <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function">T <span class="title">foo</span><span class="params">(T, <span class="type">unsigned</span> <span class="type">int</span>*)</span></span>;    <span class="comment">// 非法，inline 位置错误</span></span><br><span class="line"><span class="comment">// 修改如下：</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function"><span class="keyword">inline</span> T <span class="title">foo</span><span class="params">(T, <span class="type">unsigned</span> <span class="type">int</span>*)</span></span>;</span><br><span class="line"></span><br><span class="line">d) <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="built_in">f4</span>(T, T);      <span class="comment">// 非法，没有返回类型</span></span><br><span class="line"><span class="comment">// 修改如下：</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function">T <span class="title">f4</span><span class="params">(T, T)</span></span>;</span><br><span class="line"></span><br><span class="line">e) <span class="keyword">typedef</span> <span class="type">char</span> Ctype;</span><br><span class="line">    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Ctype&gt; <span class="function">Ctype <span class="title">f5</span><span class="params">(Ctype a)</span></span>;    <span class="comment">// 合法，但是模板声明的 Ctype 隐藏了类型别名 Ctype</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex16-19"><a href="#Ex16-19" class="headerlink" title="Ex16.19"></a>Ex16.19</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>  &lt;<span class="keyword">typename</span> T&gt; <span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">(<span class="type">const</span> T &amp;container)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">typename</span> T::size_type i = <span class="number">0</span>; i != container.<span class="built_in">size</span>(); ++i)</span><br><span class="line">        cout &lt;&lt; container.<span class="built_in">at</span>(i) &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    <span class="built_in">print</span>(vec);</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-20"><a href="#Ex16-20" class="headerlink" title="Ex16.20"></a>Ex16.20</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>  &lt;<span class="keyword">typename</span> T&gt; <span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">(<span class="type">const</span> T &amp;container)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">auto</span> it = container.<span class="built_in">begin</span>(); it != container.<span class="built_in">end</span>(); ++it)</span><br><span class="line">        cout &lt;&lt;  *it &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    <span class="built_in">print</span>(vec);</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-16-1-4"><a href="#Exercises-Section-16-1-4" class="headerlink" title="Exercises Section 16.1.4"></a>Exercises Section 16.1.4</h1><h2 id="Ex16-21"><a href="#Ex16-21" class="headerlink" title="Ex16.21"></a>Ex16.21</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">DebugDelete</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">DebugDelete</span>(std::ostream &amp;s = std::cerr): <span class="built_in">os</span>(s) &#123; &#125;</span><br><span class="line">    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function"><span class="type">void</span> <span class="title">operator</span><span class="params">()</span> <span class="params">(T *p)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">    </span>&#123; os &lt;&lt; <span class="string">&quot;deleting unique_ptr&quot;</span> &lt;&lt; std::endl; <span class="keyword">delete</span> p; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::ostream &amp;os;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-22"><a href="#Ex16-22" class="headerlink" title="Ex16.22"></a>Ex16.22</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">TextQuery</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">using</span> line_no = vector&lt;string&gt;::size_type;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">TextQuery</span>(ifstream &amp;in);</span><br><span class="line">    <span class="comment">// 其他成员不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">TextQuery::<span class="built_in">TextQuery</span>(ifstream &amp;in): <span class="built_in">file</span>(<span class="keyword">new</span> vector&lt;string&gt;, <span class="built_in">DebugDelete</span>())</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//...  </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex16-23"><a href="#Ex16-23" class="headerlink" title="Ex16.23"></a>Ex16.23</h2><p>在程序结束时释放 shared_ptr 指向的资源，会在程序结束时看到打印语句。</p>
<h2 id="Ex16-24"><a href="#Ex16-24" class="headerlink" title="Ex16.24"></a>Ex16.24</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="keyword">class</span> <span class="title class_">Blob</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> It&gt; <span class="built_in">Blob</span>(It b, It e);</span><br><span class="line">    <span class="comment">// 其他成员不变</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="keyword">template</span> &lt;<span class="keyword">typename</span> It&gt; Blob&lt;T&gt;::<span class="built_in">Blob</span>(It b, It e): <span class="built_in">data</span>(make_shared&lt;vector&lt;T&gt;&gt;(b, e)) &#123; &#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-16-1-5"><a href="#Exercises-Section-16-1-5" class="headerlink" title="Exercises Section 16.1.5"></a>Exercises Section 16.1.5</h1><h2 id="Ex16-25"><a href="#Ex16-25" class="headerlink" title="Ex16.25"></a>Ex16.25</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="keyword">template</span> <span class="keyword">class</span> <span class="title class_">vector</span>&lt;string&gt;;	<span class="comment">// extern 表明不在本文件中生成实例化代码，该实例化的定义会在其他文中中。</span></span><br><span class="line"><span class="keyword">template</span> <span class="keyword">class</span> <span class="title class_">vector</span>&lt;Sales_data&gt;;		<span class="comment">// 用 Sales_data 类实例化作为 vector 元素类型，在其他文件中可用 extern 声明此实例化，使用该定义。</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex16-26"><a href="#Ex16-26" class="headerlink" title="Ex16.26"></a>Ex16.26</h2><p>不可以； 当显式实例化 vector<Nodefault> 时，编译器会实例化 vector 的所有成员函数，vector 的构造函数会使用元素类型的默认构造函数来对元素进行值初始化，而 Nodefault 没有默认构造函数。</p>
<h2 id="Ex16-27"><a href="#Ex16-27" class="headerlink" title="Ex16.27"></a>Ex16.27</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="keyword">class</span> <span class="title class_">Stack</span> &#123; &#125;;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">f1</span><span class="params">(Stack&lt;<span class="type">char</span>&gt;)</span></span>;       <span class="comment">// (a)</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Exercise</span></span><br><span class="line">&#123;</span><br><span class="line">    Stack&lt;<span class="type">double</span>&gt; &amp;rsd;     <span class="comment">// (b)</span></span><br><span class="line">    Stack&lt;<span class="type">int</span>&gt; si;          <span class="comment">// (c)</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Stack&lt;<span class="type">char</span>&gt; *sc;        <span class="comment">// (d)</span></span><br><span class="line">    <span class="built_in">f1</span>(*sc);                <span class="comment">// (e)</span></span><br><span class="line">    <span class="type">int</span> iObj = <span class="built_in">sizeof</span>(Stack&lt;string&gt;);   <span class="comment">// (f)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（a） 、（b）、（c）、（f）分别进行了 Stack 对 char、double、int、string 类型的实例化，因为这些语句都需要用到其实例化的类；（d）和 （e）未发生实例化，因为在上述对应代码前就已经发生了实例化。</p>
<h1 id="Exercises-Section-16-2-1"><a href="#Exercises-Section-16-2-1" class="headerlink" title="Exercises Section 16.2.1"></a>Exercises Section 16.2.1</h1><h2 id="Ex16-32"><a href="#Ex16-32" class="headerlink" title="Ex16.32"></a>Ex16.32</h2><p>在模板参数推断阶段，编译器使用函数调用过程中的参数类型推断模板参数，找出最为匹配给定的函数调用的函数版本。</p>
<h2 id="Ex16-33"><a href="#Ex16-33" class="headerlink" title="Ex16.33"></a>Ex16.33</h2><ol>
<li>可以将一个 非 const 对象的引用或指针传递给一个 const 的引用或指针</li>
<li>数组或者函数指针转换；函数形参不是引用类型，则可对数组或函数类型的实参应用正常的指针转换</li>
</ol>
<h2 id="Ex16-34"><a href="#Ex16-34" class="headerlink" title="Ex16.34"></a>Ex16.34</h2><p>都不合法，当模板函数形参为引用类型，数组不能转换为指针。</p>
<h2 id="Ex16-35"><a href="#Ex16-35" class="headerlink" title="Ex16.35"></a>Ex16.35</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="function">T <span class="title">calc</span><span class="params">(T, <span class="type">int</span>)</span></span>;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="built_in">fcn</span>(T, T);</span><br><span class="line"><span class="type">double</span> d;   <span class="type">float</span> f;    <span class="type">char</span> c;</span><br><span class="line">a) <span class="built_in">calc</span>(c, <span class="string">&#x27;c&#x27;</span>);    <span class="comment">// 合法，T 为 char</span></span><br><span class="line">b) <span class="built_in">calc</span>(d, f);      <span class="comment">// 合法，T 为 double</span></span><br><span class="line">c) <span class="built_in">fcn</span>(c, <span class="string">&#x27;c&#x27;</span>);     <span class="comment">// 合法，T 为 char</span></span><br><span class="line">d) <span class="built_in">fcn</span>(d, f);       <span class="comment">// 非法，无法实例化 fcn(double, float)</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex16-36"><a href="#Ex16-36" class="headerlink" title="Ex16.36"></a>Ex16.36</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt; <span class="built_in">f1</span>(T, T);</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T1, <span class="keyword">typename</span> T2&gt; <span class="built_in">f2</span>(T1, T2);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>, j = <span class="number">42</span>, *p1 = &amp;i, *p2 = &amp;j;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> *cp1 = &amp;i, *cp2 = &amp;j;</span><br><span class="line">a) <span class="built_in">f1</span>(p1, p2);      <span class="comment">// T 为 int * </span></span><br><span class="line">b) <span class="built_in">f2</span>(p1, p2);      <span class="comment">// T1 为 int *，T2为 int *</span></span><br><span class="line">c) <span class="built_in">f1</span>(cp1, cp2);    <span class="comment">// T 为 const int *</span></span><br><span class="line">d) <span class="built_in">f2</span>(cp1, cp2);    <span class="comment">// T1 为 const int *，T2 为 const int *</span></span><br><span class="line">e) <span class="built_in">f1</span>(p1, cp1);     <span class="comment">// T 为 int *</span></span><br><span class="line">f) <span class="built_in">f2</span>(p1, cp1);     <span class="comment">// T1 为 int *，T2 为 const int *</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-16-2-2"><a href="#Exercises-Section-16-2-2" class="headerlink" title="Exercises Section 16.2.2"></a>Exercises Section 16.2.2</h1><h2 id="Ex16-37"><a href="#Ex16-37" class="headerlink" title="Ex16.37"></a>Ex16.37</h2><p>可以；不过需要显式指定参数类型，如<code>max&lt;double&gt;</code></p>
<h2 id="Ex16-38"><a href="#Ex16-38" class="headerlink" title="Ex16.38"></a>Ex16.38</h2><p>使用 make_shared 时，编译器无法从已有参数推断模板参数类型，这时就要显式指定模板参数类型。</p>
<h2 id="Ex16-39"><a href="#Ex16-39" class="headerlink" title="Ex16.39"></a>Ex16.39</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">compare</span>&lt;string&gt;(<span class="string">&quot;s1&quot;</span>, <span class="string">&quot;s2&quot;</span>);</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog">astralcon</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/">https://astralcon.gitee.io/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://astralcon.gitee.io/blog" target="_blank">A君的秘密基地</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/C-Primer/">C++ Primer</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/05/14/Wireshark-Lab-HTTP-v7-0/"><img class="prev-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Wireshark Lab: HTTP v7.0</div></div></a></div><div class="next-post pull-right"><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/"><img class="next-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Chapter 15: Object-Oriented Programming</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2022/05/14/Chapter-1-Getting-Started/" title="Chapter 1: Getting Started"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 1: Getting Started</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-10-Generic-Algorithms/" title="Chapter 10: Generic Algorithms"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 10: Generic Algorithms</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-11-Associative-Containers/" title="Chapter 11: Associative Containers"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 11: Associative Containers</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-12-Dynamic-Memory/" title="Chapter 12: Dynamic Memory"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 12: Dynamic Memory</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-13-Copy-Control/" title="Chapter 13: Copy Control"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 13: Copy Control</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/" title="Chapter 15: Object-Oriented Programming"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 15: Object-Oriented Programming</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/avatar.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">astralcon</div><div class="author-info__description">slow is fast.</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/astralcon" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:a842181471@qq.com.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">朝九晚五，健康生活！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-1"><span class="toc-number">1.</span> <span class="toc-text">Ex16.1</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-2"><span class="toc-number">2.</span> <span class="toc-text">Ex16.2</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-3"><span class="toc-number">3.</span> <span class="toc-text">Ex16.3</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-4"><span class="toc-number">4.</span> <span class="toc-text">Ex16.4</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-5"><span class="toc-number">5.</span> <span class="toc-text">Ex16.5</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-6"><span class="toc-number">6.</span> <span class="toc-text">Ex16.6</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-7"><span class="toc-number">7.</span> <span class="toc-text">Ex16.7</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-8"><span class="toc-number">8.</span> <span class="toc-text">Ex16.8</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-16-1-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 16.1.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-9"><span class="toc-number">1.</span> <span class="toc-text">Ex16.9</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-10"><span class="toc-number">2.</span> <span class="toc-text">Ex16.10</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-11"><span class="toc-number">3.</span> <span class="toc-text">Ex16.11</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-12"><span class="toc-number">4.</span> <span class="toc-text">Ex16.12</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-13"><span class="toc-number">5.</span> <span class="toc-text">Ex16.13</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-14"><span class="toc-number">6.</span> <span class="toc-text">Ex16.14</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-15"><span class="toc-number">7.</span> <span class="toc-text">Ex16.15</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-16"><span class="toc-number">8.</span> <span class="toc-text">Ex16.16</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-16-1-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 16.1.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-17"><span class="toc-number">1.</span> <span class="toc-text">Ex16.17</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-18"><span class="toc-number">2.</span> <span class="toc-text">Ex16.18</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-19"><span class="toc-number">3.</span> <span class="toc-text">Ex16.19</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-20"><span class="toc-number">4.</span> <span class="toc-text">Ex16.20</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-16-1-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 16.1.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-21"><span class="toc-number">1.</span> <span class="toc-text">Ex16.21</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-22"><span class="toc-number">2.</span> <span class="toc-text">Ex16.22</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-23"><span class="toc-number">3.</span> <span class="toc-text">Ex16.23</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-24"><span class="toc-number">4.</span> <span class="toc-text">Ex16.24</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-16-1-5"><span class="toc-number"></span> <span class="toc-text">Exercises Section 16.1.5</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-25"><span class="toc-number">1.</span> <span class="toc-text">Ex16.25</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-26"><span class="toc-number">2.</span> <span class="toc-text">Ex16.26</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-27"><span class="toc-number">3.</span> <span class="toc-text">Ex16.27</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-16-2-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 16.2.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-32"><span class="toc-number">1.</span> <span class="toc-text">Ex16.32</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-33"><span class="toc-number">2.</span> <span class="toc-text">Ex16.33</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-34"><span class="toc-number">3.</span> <span class="toc-text">Ex16.34</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-35"><span class="toc-number">4.</span> <span class="toc-text">Ex16.35</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-36"><span class="toc-number">5.</span> <span class="toc-text">Ex16.36</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-16-2-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 16.2.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-37"><span class="toc-number">1.</span> <span class="toc-text">Ex16.37</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-38"><span class="toc-number">2.</span> <span class="toc-text">Ex16.38</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex16-39"><span class="toc-number">3.</span> <span class="toc-text">Ex16.39</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/17/%E3%80%8A%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E6%89%8B%E5%86%8C%E3%80%8B%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9AChapter-1/" title="《算法设计手册》面试题：Chapter 1">《算法设计手册》面试题：Chapter 1</a><time datetime="2022-05-17T07:35:05.000Z" title="发表于 2022-05-17 15:35:05">2022-05-17</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-DHCP-v7-0/" title="Wireshark Lab: DHCP v7.0">Wireshark Lab: DHCP v7.0</a><time datetime="2022-05-14T07:19:53.000Z" title="发表于 2022-05-14 15:19:53">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-Ethernet-and-ARP-v7-0/" title="Wireshark Lab: Ethernet and ARP v7.0">Wireshark Lab: Ethernet and ARP v7.0</a><time datetime="2022-05-14T07:18:10.000Z" title="发表于 2022-05-14 15:18:10">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-ICMP-v7-0/" title="Wireshark Lab: ICMP v7.0">Wireshark Lab: ICMP v7.0</a><time datetime="2022-05-14T07:16:41.000Z" title="发表于 2022-05-14 15:16:41">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-NAT-v7-0/" title="Wireshark Lab: NAT v7.0">Wireshark Lab: NAT v7.0</a><time datetime="2022-05-14T07:15:13.000Z" title="发表于 2022-05-14 15:15:13">2022-05-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By astralcon</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script><script src="/blog/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'Uh3CYGE2t1Eaz4KoTkyVQv1N-gzGzoHsz',
      appKey: '1Gdg4rGlmT4Dcfv7oNiCH1q3',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://astralcon.gitee.io/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/'
    this.page.identifier = '2022/05/14/Chapter-16-Templates-And-Generic-Programming/'
    this.page.title = 'Chapter 16: Templates And Generic Programming'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>